const query = require("../db/query");
const md5 = require('md5');

//  查询用户是否存在
const CheckPhone = async (params) => {
    let sql = `select * from managementuser where phone = '${params.phone}'`;
    return await query(sql)
}

// 注册
const register = (params) => {
    let password = md5(params.password)
    // 可选择头像版本
    const sql = `insert into managementuser (nickname,phone, password,identity,avatar) values ('${params.nickname}','${params.phone}', '${password}','${params.identity}','${params.avatar}');`;
    return query(sql);
};

// 登录
const getUserList = (params) => {
    const sql = `select * from managementuser where phone='${params.phone}' and password='${params.password}'`;
    return query(sql);
};

// 验证码登录
const codelogin = (params) => {
    const sql = `select * from managementuser where phone='${params.phone}'`;
    return query(sql);
};

// 修改用户名
const uploadnickname = (params) => {
    const sql = `update managementuser set nickname = '${params.nickname}' where phone = '${params.phone}'`;
    return query(sql);
};

// 修改密码
const uploadpassword = (params) => {
    let password = md5(params.password)
    const sql = `update managementuser set password = '${password}' where phone = '${params.phone}'`;
    return query(sql);
};

// 修改头像
const uploadavatar = (params) => {
    const sql = `update managementuser set avatar = '${params.avatar}' where phone = '${params.phone}'`;
    return query(sql);
};

// 注销账号
const logoff = (params) => {
    const sql = `delete from managementuser where phone in (${params.phone});`;
    return query(sql);
};

// 修改上次登录时间
const updateLoginTime = (params) => {
    const sql = `UPDATE managementuser SET last_login = UNIX_TIMESTAMP() , last_login2 = NOW() WHERE phone = ${params.phone};`;
    return query(sql);
}

module.exports = {
    CheckPhone,//查询用户是否存在
    register,//注册
    getUserList,// 登录
    codelogin,//验证码登录
    uploadnickname,//修改用户名
    uploadpassword,//修改密码
    uploadavatar,//修改头像
    logoff,//注销账号
    updateLoginTime,//修改上次登录时间
}